package com.ms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;

/**
 * Created by RAZANERO on 24/02/14.
 */
public class StringUtil {
    private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);


    public static String readerXmlError(File xml) {
        String parserClass = "org.apache.xerces.parsers.SAXParser";
        String validationFeature
                = "http://xml.org/sax/features/validation";
        String schemaFeature
                = "http://apache.org/xml/features/validation/schema";
        MyErrorHandler me = new MyErrorHandler();
        try {

            Reader reader = new InputStreamReader(new FileInputStream(xml), "UTF-8");
            XMLReader r = XMLReaderFactory.createXMLReader(parserClass);
            r.setFeature(validationFeature, true);
            r.setFeature(schemaFeature, true);

            r.setErrorHandler(me);
            r.parse(new InputSource(reader));
            return me.getSb().toString();
        } catch (Exception e) {
            LOG.error("Error al leer xml ", e);
        }
        return me.getSb().toString();
    }

    private static class MyErrorHandler extends DefaultHandler {
        StringBuilder sb = new StringBuilder();

        public StringBuilder getSb() {
            return sb;
        }

        public void warning(SAXParseException e) throws SAXException {

        }

        public void error(SAXParseException e) throws SAXException {

        }

        public void fatalError(SAXParseException e) throws SAXException {
            sb.append("Linea:" + e.getLineNumber() + " Columna: " + e.getColumnNumber() + " mensaje:" + e.getMessage());
        }
    }

    public static boolean hasText(String str) {
        return StringUtils.hasText(str);
    }

    public static String lpad(Object str, int size, char filler) {
        return padIntern(str == null ? "" : String.valueOf(str), size, filler, false);
    }

    private static String padIntern(String str, int size, char filler, boolean right) {
        if (str == null) str = "";
        if (str.length() > size) str = str.substring(0, size - 1);
        if (str.length() < size) {
            StringBuffer strBuf = new StringBuffer(size);
            strBuf.append(str);
            while (strBuf.length() < size) {
                if (right) strBuf.append(filler);
                else strBuf.insert(0, filler);
            }
            str = strBuf.toString();
        }
        return str;
    }

    public static boolean equals(String str1, String str2) {
        return nvl(str1, "").equals(nvl(str2, ""));
    }

    public static String nvl(String s1, String s2) {
        if (s1 != null) return s1;
        else return s2;
    }

    public static String concat(Collection list, String sep) {
        return concat(list, sep, null);
    }


    public static String concat(Collection list, String sep, String enclosing) {
        StringBuffer buf = new StringBuffer();
        for (Object str : list) {
            if (buf.length() > 0) buf.append(sep);
            if (enclosing != null) buf.append(enclosing);
            buf.append(str);
            if (enclosing != null) buf.append(enclosing);
        }
        return buf.toString();
    }

    public static String concat(Collection list) {
        return concat(list, ",", "");
    }

}
